package com.intelligent.health.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.intelligent.health.member.dao.UserMapper;
import com.intelligent.health.member.dto.UserDto;
import com.intelligent.health.member.pojo.Permission;
import com.intelligent.health.member.pojo.Role;
import com.intelligent.health.member.pojo.User;
import com.intelligent.health.member.service.MemberService;
import com.intelligent.health.member.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/17/22:59
 * @Description: MemberServiceImpl
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addMember(User user) {
        user.setStation(1);
        user.setRegisterTime(new Date(System.currentTimeMillis()));
        String encodePwd = new BCryptPasswordEncoder().encode(user.getPassword());
        //加密后的用户密码
        user.setPassword(encodePwd);
        userMapper.insert(user);
    }

    @Override
    public IPage<UserVo> queryMembersByPage(UserDto userDto, Integer pageNum, Integer pageSize) {
        IPage<UserVo> userIPage = userMapper.selectPage(new Page<>(pageNum, pageSize), getQueryWrapper(userDto));
        return userIPage;
    }

    @Override
    public UserVo queryMemberById(Long userId) {
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userMapper.selectById(userId), userVo);
        return userVo;
    }

    @Override
    public void editMemberInfo(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        userMapper.updateById(user);
    }

    @Override
    public void delMember(Long userId) {
        userMapper.setStation(userId);
    }

    @Override
    public HashMap<String, Object> getUserAuthInfo(String username) {
        HashMap<String, Object> map = new HashMap<>();
        User user = userMapper.queryUserAuthInfo(username);
        map.put("username", username);
        map.put("password", user.getPassword());
        ArrayList<String> roleBos = new ArrayList<>();
        Set<String> permissions = new HashSet<>();
        for (Role role : user.getRoles()) {
            for (Permission permission : role.getPermissions()) {
                permissions.add(permission.getKeyword());
            }
            roleBos.add(role.getKeyword());
        }
        map.put("roleBos", roleBos);
        map.put("permissions", permissions);
        return map;
    }

    @Override
    public void delBatchMember(ArrayList<Long> userIds) {
        userMapper.delBatchMember(userIds);
    }

    public static LambdaQueryWrapper getQueryWrapper(UserDto userDto) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(userDto.getUsername())) {
            queryWrapper.like(User::getUsername, userDto.getUsername());
        }
        if (StringUtils.isNotBlank(userDto.getNickName())) {
            queryWrapper.like(User::getNickName, userDto.getNickName());
        }
        if (StringUtils.isNotBlank(userDto.getGender())) {
            queryWrapper.eq(User::getGender, userDto.getGender());
        }
        if (userDto.getAge() != null) {
            queryWrapper.eq(User::getAge, userDto.getAge());
        }
        if (StringUtils.isNotBlank(userDto.getTelephone())) {
            queryWrapper.eq(User::getTelephone, userDto.getTelephone());
        }
        if (StringUtils.isNotBlank(userDto.getEmail())) {
            queryWrapper.eq(User::getEmail, userDto.getEmail());
        }
        //前端默认 有效 1
        if (userDto.getStation() != null) {
            queryWrapper.eq(User::getStation, userDto.getStation());
        }
        return queryWrapper;
    }
}
